# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import pymysql
import scrapy



# class MoocPipeline:

#     conn = None
#     cur = None
#     # 重写父类，该方法只会在爬虫开始时进行一次
#     def open_spider(self,spider):
#         print('mysql开始爬虫')
#         self.conn = pymysql.Connect(host="124.220.0.182",user='root',password='123456',database='scrapy',port=3306,charset='utf8')
        

#     # 有几个item对象，就会调用几次
#     def process_item(self, item, spider):
#         name = item['name']
#         url = item['url']
#         content = item['content']
#         logo_name= item['name']+'.jpg'
#         image_urls= item['image_urls']
        

#         self.cur = self.conn.cursor()
#         try:
#             self.cur.execute("insert into university (name, url, content, logo_name, image_urls) values(%s, %s, %s ,%s, %s)",(name, url, content,logo_name, image_urls))
#             self.conn.commit()
#         except Exception as e:
#             print(e)
#             self.conn.rollback()
#         return item #该item会传递给下一个管道类
    
#     # 重写父类，该方法只会在爬虫结束时进行一次
#     def close_spider(self,spider):
#         print('mysql结束爬虫')
#         self.conn.close()
        
from scrapy.pipelines.images import ImagesPipeline

class DownImagsPipeline(ImagesPipeline):
    
    # 对item中的图片进行请求操作
    def get_media_requests(self, item, info):
        yield scrapy.Request(item['src'])

    # 定制图片路径
    def file_path(self, request, response=None, info=None, *, item=None):
        return item['name']+'.jpg'

    # 完成后将item返回给下一个管道类
    def item_completed(self, results, item, info):
        return item

